#include "passwd.h"
#include "ui_passwd.h"
#include "url_string.h"
#include <QNetworkRequest>
#include <QNetworkReply>
#include "common.h"

extern std::string      g_token;
extern std::string      g_url;

Passwd::Passwd(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::passwd),
    m_manager(new QNetworkAccessManager(this))
{
    ui->setupUi(this);
    connect(m_manager, SIGNAL(finished(QNetworkReply*)),
            this, SLOT(finished(QNetworkReply*)));
    setWindowTitle(_T("修改系统密码"));
}

Passwd::~Passwd()
{
    delete ui;
    delete m_manager;
}

void Passwd::clickOK()
{
    if(ui->m_pwd1->text() != ui->m_pwd2->text())
    {
        ui->m_error->setText(_T("<font color=red>两次输入的密码不一致</font>"));
        ui->m_pwd1->setFocus();
        return;
    }

    if("" == ui->m_pwd1->text())
    {
        ui->m_error->setText(_T("<font color=red>请输入要修改的新密码</font>"));
        ui->m_pwd1->setFocus();
        return;
    }

    url_string<>        url;

    url.format("%s?handler=pwd&token=%u&pwd=%u",
               g_url.c_str(),
               g_token.c_str(),
               ui->m_pwd1->text().toStdString().c_str());
    m_manager->get(QNetworkRequest(QUrl(url.c_str())));
}

void Passwd::finished(QNetworkReply *reply)
{
    accept();
}
